package UI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import MainLaunch.Main;
import MainLaunch.StaticFields;
import VideoTools.VideoPlayer;


@SuppressWarnings("serial")
public class BarDeProgression extends JFrame implements Runnable
{
	
	private static final int margeEnHaut=28;
	private static final int margeEnBas=25;
	private static final int marge = 4;
	
	private JProgressBar progress;
	private int value;
	
	//  lecture de la taille de l'écran
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
	Toolkit toolkit = Toolkit.getDefaultToolkit(); 		
 	
 	private Image image = toolkit.getImage(StaticFields.ressourcePath + "new_logo_devint.JPG");
	
 	public BarDeProgression (){
 		super ("Lancement...");
		
		this.setSize(457,373);
		this.value = 0;
		this.progress = new JProgressBar (0, 100);
		this.progress.setStringPainted (true);
		this.getContentPane ().add (this.progress,BorderLayout.SOUTH);
		this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		this.setLocation ((screenSize.width-getWidth())/2,(screenSize.height-getHeight())/2);
		this.lanceLongTraitement();
		try   {
	    	MediaTracker tracker = new MediaTracker(this);
	      	tracker.addImage(image,0);
	    	tracker.waitForID(0);
    	} 
    	catch (Exception e) {
    		System.out.println("Erreur de chargement");
    	}
    	this.setVisible (true);
	}


	public void paint(Graphics g){
		Graphics2D graphics = (Graphics2D) g;
		graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
		g.drawImage(image, marge,margeEnHaut,this.getSize().width-(2*marge),this.getSize().height-margeEnHaut-marge-margeEnBas,this);

	}



	// cree un nouveau Thread et appelle la methode start
	// Cf une documentation sur les Thread si vous ne comprennez pas le
	// mecanisme ....
	public void lanceLongTraitement ()
	{
		Thread t = new Thread (this);
		t.start ();
		
		// maintenant nous rendons la main au processus d'evenement
	}

	// methode de l'interface Runnable
	// lance un nouveau thread qui va executer le code de la methode longTraitement
	public void run (){
		this.longTraitement ();
	}


	public void longTraitement ()
	{	// execute la succession d'operation ... 
		// on est plus dans le processus d'evenement mais dans le nouveau Thread
		for (;;)
		{	this.uneOperation ();
			// maintenant on appelle la methode pour mettre a jour la barre
			// de progression
			this.majProgress (); 
			repaint();
			
			//	ensuite on ouvre la fenetre vrai!!
			if (VideoPlayer.getIsLaunch()==true){
				this.dispose();
				Main.setEnabledWindow(true);
				
				break;
			}
		}
		
		
	}



	// methode qui effectue une pause pour simuler une operation
	private synchronized void uneOperation ()
	{	try
		{	this.wait (10);
		}
		catch (InterruptedException e)
		{
		}
	}


	public void majProgress ()
	{	if ( SwingUtilities.isEventDispatchThread () )
		{	progress.setValue (++value);
		}
		else
		{	Runnable callMAJ = new Runnable ()
			{	public void run ()
				{	majProgress ();
                }
            };
            SwingUtilities.invokeLater (callMAJ);
		}
	}

}
